﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class KDHelper {
	
	public static KDTree TreeFromUnitPars(List<Warrior> units){
		int n = units.Count;
		if(n > 0){
			Vector3[] positions = new Vector3[n];
			for(int i=0; i<n; i++){
				positions[i] = units[i].transform.position;
			}
			return KDTree.MakeFromPoints(positions);
		}
		return null;
	}
	
	public static Warrior FindNearestUP(Vector3 origin, List<Warrior> allUnits, KDTree kd){
		if(kd != null){
			int i = kd.FindNearest(origin);
			if(i >= 0 && i < allUnits.Count){
				return allUnits[i];
			}
		}
		return null;
	}




    public static Warrior[] FindNearestK(Vector3 origin,List<Warrior> allUnits,KDTree kd,int k)
    {
        
        if (kd != null)
        {
            int[] ki = kd.FindNearestsK(origin, k);
            Warrior[] t = new Warrior[ki.Length];
            for (int i=0;i<ki.Length;i++)
            {
                t[i] = allUnits[ki[i]];
            }
            return t;
        }
        return null;
    }
}
